package com.ruoyi.iot.utils;


import com.ruoyi.iot.domain.IotMasterConfig;
import com.ruoyi.iot.modbus.tcp.SerialTcp;
import com.serotonin.modbus4j.ModbusMaster;
import lombok.Getter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ModbusCache {
    /**
     * 串口信息缓存Map
     * 查询串口缓存
     */
    @Getter
    private static Map<String, IotMasterConfig> iotMasterConfigMap = new HashMap<String, IotMasterConfig>();
    /**
     * 串口信息缓存Map
     */
    @Getter
    private static Map<String, SerialTcp> serialTcpMap = new HashMap<String, SerialTcp>();
    /**
     * 串口读取主机
     */
    @Getter
    private static Map<String, ModbusMaster> modbusMasterMap = new HashMap<>();

    /**
     * 查询串口缓存
     *
     * @return map
     */
    public static IotMasterConfig getIotMasterConfig(String key) {
        return iotMasterConfigMap.get(key);
    }

    /**
     * 设置串口缓存
     *
     * @param list
     */
    public static void setSerialPortMap(List<IotMasterConfig> list) {
        for (IotMasterConfig iotMasterConfig : list) {
            iotMasterConfigMap.put(iotMasterConfig.getSerialPort(), iotMasterConfig);
        }
    }


    public static SerialTcp getSerialTcp(String key) {
        return serialTcpMap.get(key);
    }

    /**
     * 设置tcp地址缓存
     *
     * @param list
     */
    public static void setSerialTcpMap(List<SerialTcp> list) {
        for (SerialTcp serialTcp : list) {
            serialTcpMap.put(serialTcp.getHost(), serialTcp);
        }
    }

    public static ModbusMaster getModbusMaster(String key) {
        return modbusMasterMap.get(key);
    }

    public static void setModbusMasterMap(Map<String, ModbusMaster> modbusMasterMap) {
        ModbusCache.modbusMasterMap = modbusMasterMap;
    }

}
